package bsi.vn.Data;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import bsi.vn.FirstApp.R;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ContractAdapter extends ArrayAdapter<ContractData>{

	private Context context;
	private int resID;
	private ArrayList<ContractData> list;
	
	public ContractAdapter(Context context, int textViewResourceId,
			List<ContractData> objects) {
		super(context, textViewResourceId, objects);
		this.context = context;
		resID = textViewResourceId;
		list = (ArrayList<ContractData>) objects;
	}

	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null) 
        { 
            LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
            convertView=inflater.inflate(R.layout.contract_detail_item, null);                     
        }
		TextView cNo = (TextView) convertView.findViewById(R.id.tvContractNo);
		TextView cus = (TextView) convertView.findViewById(R.id.tvCustomer);
		TextView stt = (TextView) convertView.findViewById(R.id.tvStatus);
			
		ContractData s = list.get(position);
		String status = s.getExpireDate();
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		Calendar calendar = getDate(status);
		Calendar now = Calendar.getInstance();

		
//		Log.d("test", now.getTime().toString());
//		Log.d("test", calendar.getTime().toString());
		if(calendar.getTime().after(now.getTime())){
			stt.setText(R.string.sacSttInterm);
			stt.setTextColor(Color.GREEN);
		}else{
			stt.setText(R.string.sacSttExpired);
			stt.setTextColor(Color.RED);
		}
		
		cNo.setText(s.getContractNo());
		cus.setText(s.getCustomer());
		
		
		return convertView;
		
	}
	public static Calendar getDate(String s){
		String sub = s.substring(0, 8);
		String sday = sub.substring(0, 2);
		String smonth = sub.substring(2, 4);
		String syear = sub.substring(4, 8);
//		SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		Calendar c = Calendar.getInstance();
		c.set(Integer.parseInt(syear), Integer.parseInt(smonth), Integer.parseInt(sday));
//		Log.d("test", "day = " + sday + " mounth = " + smonth + " year = "+syear);
		return c;
		
	}
	


}
